package com.yz.jdd.widget.keyboard;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;

import com.yz.jdd.R;

import java.util.List;

/**
 * 项目名称：Maneki
 * 创建人：zx
 * 创建时间：2016/12/28-上午10:06
 */

public class MyKeyBoardView extends KeyboardView {
    private Context mContext;
    private Keyboard mKeyBoard;
    private String strOkbutton = "";
    private String strClearButton = "清空";
    private String strBtnPatType1 = "现金";//除扫码支付外 多出一种支付方式
    private String strBtnPatType2 = "银行卡";//多除两种或以上
    private int buttonNormalColor = 0xffFF211A;
    private int buttonColor = 0xffFF211A;
    private int textClearColor = 0xff333333;
    private int textColor = Color.WHITE;
    private int buttonPressedColor = buttonNormalColor;
    public static int DEFAULT_WIDTH = 720;
    public static int DEFAULT_HEIGHT = 1280;
    private int paintTextSize = 36;
    private int keyHeight = 107;

    public MyKeyBoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        paintTextSize = getTextSizeWithScreen();
    }

    public MyKeyBoardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
    }

    public void setOkButton(String strOkbutton) {
        if (!TextUtils.isEmpty(strOkbutton)) {
            this.strOkbutton = strOkbutton;
        }
    }

    /**
     * 收银键盘下方最多三个按钮 此方法设置前两个按钮文案
     *
     * @param btnStr1
     * @param btnStr2
     */
    public void setPayTypeButton(String btnStr1, String btnStr2) {
        this.strBtnPatType1 = btnStr1;
        this.strBtnPatType2 = btnStr2;
    }

    public void setClearButton(String strClearButton) {
        if (!TextUtils.isEmpty(strClearButton)) {
            this.strClearButton = strClearButton;
        }
    }

    public void setOkButtonColor(int buttonNormalColor, int buttonPressedColor, int textColor) {
        this.buttonNormalColor = buttonNormalColor;
        this.buttonPressedColor = buttonPressedColor;
        this.textColor = textColor;
        invalidate();
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mKeyBoard = this.getKeyboard();
        List<Key> keys = null;
        if (mKeyBoard != null) {
            keys = mKeyBoard.getKeys();
        }

        if (keys != null) {
            keyHeight = keys.get(0).height;
            StateListDrawable sd = addStateListDrawable(buttonNormalColor, buttonPressedColor);

            for (Key key : keys) {
                // 数字键盘的处理，清除键的处理4896
                if (key.codes[0] == -4) {
                    if (sd != null) {
                        drawKeyBackground(sd, canvas, key);
                    } else {
                        drawKeyBackground(mContext.getResources().getDrawable(R.drawable.bg_keyboardview_yes), canvas, key);
                    }
                    drawText(canvas, key, textColor, strOkbutton);
//                } else if (key.codes[0] == Integer.valueOf(getResources().getString(R.string.clear_code))) {
//                    drawText(canvas, key, textClearColor, strClearButton);
                } else if (key.codes[0] == Integer.valueOf(getResources().getString(R.string.pay_type_1_code))) {
                    drawText(canvas, key, buttonColor, strBtnPatType1);
                } else if (key.codes[0] == Integer.valueOf(getResources().getString(R.string.pay_type_2_code))) {
                    drawText(canvas, key, buttonColor, strBtnPatType2);
                }
            }
        }

    }

    /**
     * key高度
     *
     * @return
     */
    public int getkeyHeigth() {
        return keyHeight;
    }

    private void drawKeyBackground(Drawable npd, Canvas canvas, Keyboard.Key key) {

        int[] drawableState = key.getCurrentDrawableState();
        if (key.codes[0] != 0) {
            npd.setState(drawableState);
        }
        npd.setBounds(key.x, key.y, key.x + key.width, key.y
                + key.height);
        npd.draw(canvas);
    }

    private void drawText(Canvas canvas, Keyboard.Key key, int color, String str) {

//        Rect bounds = new Rect();
        Paint paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setStrokeWidth(1f);
        paint.setAntiAlias(true);
        paint.setTextSize(paintTextSize);
        //paint.setStyle(Paint.Style.FILL);
        //paint.setFakeBoldText(false);

        paint.setColor(color);
        if (!TextUtils.isEmpty(str)) {
            // String label = key.label.toString();
            // Field field;
//
//
//            if (label.length() > 1 && key.codes.length < 2) {
//                int labelTextSize = 0;
//                try {
//                    field = KeyboardView.class.getDeclaredField("mLabelTextSize");
//                    field.setAccessible(true);
//                    labelTextSize = (int) field.get(this);
//                } catch (NoSuchFieldException e) {
//                    e.printStackTrace();
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//                //paint.setColor(Color.RED);
//                //paint.setTextSize(labelTextSize);
//                //paint.setTypeface(Typeface.DEFAULT);
//
//            } else {
//
//                int keyTextSize = 0;
//                try {
//                    field = KeyboardView.class.getDeclaredField("mLabelTextSize");
//                    field.setAccessible(true);
//                    keyTextSize = (int) field.get(this);
//                } catch (NoSuchFieldException e) {
//                    e.printStackTrace();
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//                //paint.setColor(Color.RED);
//                //paint.setTextSize(keyTextSize);
//                //paint.setTypeface(Typeface.DEFAULT);
//
//            }


            // paint.getTextBounds(key.label.toString(), 0, key.label.toString()
            //       .length(), bounds);
            //绘制的位置
            //canvas.drawText(key.label.toString(),key.x+key.width);
            canvas.drawText(str, key.x + key.width / 2,
                    key.y + key.height / 2 + paint.getTextSize() / 2 - 2, paint);
        } else if (key.icon != null) {
            key.icon.setBounds(key.x + (key.width - key.icon.getIntrinsicWidth()) / 2, key.y + (key.height - key.icon.getIntrinsicHeight()) / 2,
                    key.x + (key.width - key.icon.getIntrinsicWidth()) / 2 + key.icon.getIntrinsicWidth(), key.y + (key.height - key.icon.getIntrinsicHeight()) / 2 + key.icon.getIntrinsicHeight());
            key.icon.draw(canvas);
        }

    }

    public StateListDrawable addStateListDrawable(int idNormal, int idPressed) {
        StateListDrawable sd = new StateListDrawable();
        Drawable normal = idNormal == -1 ? null : new ColorDrawable(idNormal);
        Drawable pressed = idPressed == -1 ? null : new ColorDrawable(idPressed);
        sd.addState(new int[]{android.R.attr.state_pressed}, pressed);
        sd.addState(new int[]{}, normal);
        return sd;
    }

    public int getTextSizeWithScreen() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;
        float rate = (float) screenWidth / DEFAULT_WIDTH;
//36为默认大小 通过乘以比例算出当前字体大小

        return (int) (paintTextSize * rate);

    }
}
